// pages/analyse/analyse.js
const db = wx.cloud.database();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    sex: 'male',
    weight: 50,
    height: 170,
    BMIdata: 20,
    adviceMsg: '',
    showadviceview: 'none',
    bodyurl: '',
    bodyUrls: {
      male: [
        'cloud://main-wc2xz.6d61-main-wc2xz-1259126940/daquan/images/BMI/man-thinnish.png',
        'cloud://main-wc2xz.6d61-main-wc2xz-1259126940/daquan/images/BMI/man-healthy.png',
        'cloud://main-wc2xz.6d61-main-wc2xz-1259126940/daquan/images/BMI/man-overweight.png',
        'cloud://main-wc2xz.6d61-main-wc2xz-1259126940/daquan/images/BMI/man-fat.png'
      ],
      female: [
        'cloud://main-wc2xz.6d61-main-wc2xz-1259126940/daquan/images/BMI/woman-thinnish.png',
        'cloud://main-wc2xz.6d61-main-wc2xz-1259126940/daquan/images/BMI/woman-healthy.png',
        'cloud://main-wc2xz.6d61-main-wc2xz-1259126940/daquan/images/BMI/woman-overweight.png',
        'cloud://main-wc2xz.6d61-main-wc2xz-1259126940/daquan/images/BMI/woman-fat.png'
      ]
    },
    bodySexUrls: [],
    saveflag: false //是否保存数据到数据库中
  },
  toReportPage() {
    wx.navigateTo({
      url: './report/report',
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      bodySexUrls: this.data.bodyUrls.male,
      bodyurl: this.data.bodyUrls.male[1]
    })
  },
  radioChange(e) {
    this.setData({
      sex: e.detail.value,
      showadviceview: 'none'
    })
    if (this.data.sex == 'male') {
      this.setData({
        bodySexUrls: this.data.bodyUrls.male,
        bodyurl: this.data.bodyUrls.male[1]
      })
    } else {
      this.setData({
        bodySexUrls: this.data.bodyUrls.female,
        bodyurl: this.data.bodyUrls.female[1]
      })
    }
  },
  changeImg() {
    let result = this.data.result
    this.setData({
      bodyurl: this.data.bodySexUrls[result - 1]
    });
  },
  weightSlider(e) {
    this.setData({
      weight: parseFloat(e.detail.value.toFixed(2))
    })
    this.getBMIdata();
    this.setData({
      showadviceview: 'none'
    })
  },
  heightSlider(e) {
    this.setData({
      height: e.detail.value
    })
    this.getBMIdata();
    this.setData({
      showadviceview: 'none'
    })
  },
  weightInput(e) {
    this.setData({
      weight: e.detail.value
    })
    this.getBMIdata();
    this.setData({
      showadviceview: 'none'
    })
  },
  heightInput(e) {
    this.setData({
      height: e.detail.value
    })
    this.getBMIdata();
    this.setData({
      showadviceview: 'none'
    })
  },
  getBMIResutl() {
    var that = this;
    wx.showModal({
      title: '是否保存',
      content: '是否保存为您今日的身体数据？',
      success: res => {
        if (res.confirm) {
          that.data.saveflag = true;
        } else if (res.cancel) {
          that.data.saveflag = false;
        }
        that.getBMIdata();
        that.setData({
          showadviceview: 'block'
        })
        if (that.data.saveflag) {
          wx.cloud.callFunction({
            name: 'saveBodyData',
            data: {
              weight: that.data.weight,
              height: that.data.height,
              BMI: that.data.BMIdata,
              result: that.data.result
            },
            success: res => {
              console.log(res)
            }
          })
        }
      },
      fail(err) {
        console.log(err)
      }
    })
  },
  getBMIdata() {
    var that = this;
    wx.cloud.callFunction({
      name: 'calBMI',
      data: {
        weight: that.data.weight,
        height: that.data.height
      },
      success: res => {
        //console.log(res);
        that.setData({
          BMIdata: res.result.BMIdata,
          adviceMsg: res.result.returnMsg,
          minweight: res.result.minWeight,
          maxweight: res.result.maxWeight,
          result: res.result.result,
        })
        that.changeImg();
      },
      fail(err) {
        console.log(err)
      }
    })

  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})